let util = require('util');
let mysqlPool = require('./MySQLPool');
class UnionInfoDao {

    constructor() {
    }

    async updateUnionRoyaltySetting(unionId, setting){
        let sql = 'update unioninfo set royaltySetting = ? where unionId = ?';
        let args = [setting, unionId];
        let res = await mysqlPool.query(sql, args);
    }

    async getUnionInfo(unionId){
        let sql = 'select * from unioninfo  where unionId = ?';
        let args = [unionId];
        let res = await mysqlPool.query(sql, args);
        if(res && res.length > 0){
            return res[0];
        }
        return null;
    }

    /*async saveUnionRoyaltyRecord(record, conn){
        if(!conn || conn === undefined){
            conn = mysqlPool;
        }
        let sql = 'insert into union_royalty_record (unionId, memberUid, totalAmount, royaltyRate, royaltyAmount, roomId, gameType, state, createTime) values (?, ?, ?, ?, ?, ?, ?, ?, ?)';
        let args = [record.unionId, record.memberUid, record.totalAmount, record.royaltyRate, record.royaltyAmount, record.roomId, record.gameType, 0, new Date()];
        let res = await conn.query(sql, args);
        if(res){
            return record;
        }
    }*/
    async saveUnionRoyaltyRecord(record, conn){
        if(!conn || conn === undefined){
            conn = mysqlPool;
        }
        let sql = 'insert into union_royalty_record (unionId, memberUid, totalAmount, royaltyRate, royaltyAmount, roomId, gameType, state, clubId, clubRoyaltyRate, clubRoyaltyAmount, gameSort, createTime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
        let args = [record.unionId, record.memberUid, record.totalAmount, record.royaltyRate, record.royaltyAmount, record.roomId, record.gameType, 0, record.clubId, record.clubRoyaltyRate, record.clubRoyaltyAmount, record.gameSort, record.createTime];
        let res = await conn.query(sql, args);
        if(res){
            return record;
        }
    }

    async getUnionRoyaltyRecordList(unionId, startTime, endTime){
        let sql = 'select a.*, b.name, b.headimgurl from union_royalty_record a left join users b on a.memberUid = b.uid where a.unionId = ? and a.createTime > ? and a.createTime < ? order by a.createTime desc';
        let args = [unionId, startTime, endTime];
        return await mysqlPool.query(sql, args);
    }

    async getUnionClubIdList(unionId){
        let sql = 'select clubId from clubinfo where unionId = ? and unionState != -1 and rel = 0';
        let args = [unionId];
        let clubIds = await mysqlPool.query(sql, args);
        return clubIds;
    }

    //获取联盟内某个俱乐部的抽水列表
    async getUnionClubRoyaltyRecordList(unionId, clubId, startTime, endTime){
        let sql = 'select a.*, b.name, b.headimgurl from union_royalty_record a left join users b on a.memberUid = b.uid where a.unionId = ? and a.clubId = ? and a.createTime > ? and a.createTime < ? order by a.createTime desc, a.gameSort';
        let args = [unionId, clubId, startTime, endTime];
        let clubRecord = await mysqlPool.query(sql, args);
        return this.pakageClubRoyaltyRecord(clubRecord);
    }

    //对数据库的抽水记录按照房间号进行封装
    pakageClubRoyaltyRecord(clubRecord){
        let roomList = [];
        //先将抽水记录按照房间进行归类
        for(let i in clubRecord){
            let roomId = clubRecord[i].roomId;
            if(!roomList[roomId]){
                roomList[roomId] = [];
            }
            roomList[roomId].push(clubRecord[i]);
        }

        let clubRecordList = [];
        //再根据不同的房间（即场次）进行数据封装
        for(let roomId in roomList){
            let roomTotalAmount= 0;
            let roomInfo = {"roomId":roomId, "time": roomList[roomId][0].createTime, "unionRoyaltyRate": roomList[roomId][0].royaltyRate, "gameType": roomList[roomId][0].gameType};
            let roomRecordList = roomList[roomId];
            for(let j in roomRecordList){
                roomTotalAmount = roomTotalAmount + roomRecordList[j].royaltyAmount;
            }
            roomInfo["roomTotalAmount"] = roomTotalAmount;
            clubRecordList.push({"roomInfo":roomInfo, "roomRecordList": roomRecordList});
        }
        clubRecordList.sort(function(a, b){
            return new Date(b.roomInfo.time).getTime() - new Date(a.roomInfo.time).getTime();
        })
        return clubRecordList;
    }


}

let unionInfoDao = new UnionInfoDao();
module.exports = unionInfoDao;